package m.jclecai.com.ui;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import m.jclecai.com.R;
import m.jclecai.com.bean.OpenListBean;

/**
 * Created by andy on 2017/8/25.
 */
public class KSHistoryAdapter extends BaseAdapter {


    private final List<OpenListBean.Records> mDataList;
    private final Context mContext;

    public KSHistoryAdapter(Context context, List<OpenListBean.Records> bean) {

        mContext = context;
        mDataList = bean;
    }


    @Override
    public int getCount() {
        return mDataList.size();
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View view = convertView;
        ViewHolder viewHolder;
        if (convertView == null) {

            view = View.inflate(mContext, R.layout.item_ks_history, null);
            viewHolder = new ViewHolder(view);


            view.setTag(viewHolder);
        } else {
            viewHolder = ((ViewHolder) view.getTag());
        }

        OpenListBean.Records records = mDataList.get(position);
        ImageView[] dices = new ImageView[3];
        dices[0] = viewHolder.mIvLotteryKsOne;
        dices[1] = viewHolder.mIvLotteryKsTwo;
        dices[2] = viewHolder.mIvLotteryKsThree;

        viewHolder.mTvTerm.setText(records.getTerm().substring(records.getTerm().length() - 2) + "期");

        String[] split = records.getResult().split(",");

        viewHolder.mTvKsType.setText(speciationAnalysis(records.getResult()));
        viewHolder.mTvSumNumber.setText(Integer.valueOf(split[0])+Integer.valueOf(split[1])+Integer.valueOf(split[2])+"");

        viewHolder.mTvDiceNumber.setText(" "+split[0]+" "+split[1]+" "+split[2]);
        setAnnouncementInfo(split,dices);

        return view;
    }


    int[] dicesRes = new int[]{R.drawable.dice1, R.drawable.dice2, R.drawable.dice3, R.drawable.dice4, R.drawable.dice5, R.drawable.dice6};
    //快三的具体显示；
    private void setAnnouncementInfo(String[] dicesStr,ImageView[] ivDices){

        //这里我们是需要根据dicesStr来决定ivDices具体显示什么；
        List<Integer> list=new ArrayList<>();
        if (dicesStr != null) {
            for (String s : dicesStr) {
                list.add(Integer.valueOf(s));
            }
        }
        for (int i = 0; i < ivDices.length; i++) {
            ivDices[i].setImageResource(dicesRes[list.get(i)-1]);
        }

    }


    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    static class ViewHolder {
        @BindView(R.id.tv_term)
        TextView mTvTerm;
        @BindView(R.id.iv_lottery_ks_one)
        ImageView mIvLotteryKsOne;
        @BindView(R.id.iv_lottery_ks_two)
        ImageView mIvLotteryKsTwo;
        @BindView(R.id.iv_lottery_ks_three)
        ImageView mIvLotteryKsThree;
        @BindView(R.id.tv_sum_number)
        TextView mTvSumNumber;
        @BindView(R.id.tv_ks_type)
        TextView mTvKsType;

        @BindView(R.id.tv_dice_number)
        TextView mTvDiceNumber;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    //根据CODE判断形态
    private String speciationAnalysis(String code) {
        String form="";

        String[] split = code.split(",");
        Integer one = Integer.valueOf(split[0]);
        Integer two = Integer.valueOf(split[1]);
        Integer three = Integer.valueOf(split[2]);

        if (one == two) {
            form = "二同号";
            if (two == three) {
                form = "三同号";
            }
        } else {
            if (two == three) {
                form = "二同号";
            }
        }

        if (one == two - 1 && two == three - 1) {
            form = " 三连号";
        } else if (one != two && two != three) {
            form = "三不同号";
        }
        return form;
    }

}
